package com.wjq.boot.service.impl;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjq.boot.entity.Role;
import com.wjq.boot.entity.TAdmin;
import com.wjq.boot.mapper.RoleMapper;
import com.wjq.boot.mapper.UploadFileMapper;
import com.wjq.boot.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjq.boot.service.UploadFileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wangjiaquan
 * @since 2024-08-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UploadFileService uploadFileService;

    @Override
    public Boolean fixState(Long id, Boolean state) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("state",state ? 0 : 1);
        return roleMapper.update(null,updateWrapper) > 0;
    }

    @Override
    public Page<Role> getTargetPage(Integer pageNo, Integer pageSize, String keyword) {
        Page<Role> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword);
        return roleMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Boolean add(String name, String description, MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 接收到文件后获取文件的后缀名和生成的名字组合，然后给他存到服务器中，然后将这个图片的文件名写到数据库中即可
        // 与文件服务器建立连接
        // 保存文件名称
        if(file.getSize() == 0){
            return this.save(new Role(name,description));
        }
        return this.save(new Role(name,description,uploadFileService.upload("images",file)));
    }

    @Override
    public Boolean check(Long id, String value, String column) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        Long count;
        if(id != null){
            count = count(queryWrapper.eq(column, value).ne("id",id));
        } else {
            count = count(queryWrapper.eq(column, value));
        }
        // 大于零返回false 等于零返回true
        return !(count > 0);
    }

    @Override
    public Boolean update(Long id, String name, String description, MultipartFile file, String icno) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        if(name != null){
            updateWrapper.set("name",name);
        }
        if(description != null){
            updateWrapper.set("description",description);
        }
        if(file != null){
                updateWrapper.set("icno", uploadFileService.upload("images",file));
        } else {
            updateWrapper.set("icno",icno);
        }
        return roleMapper.update(null,updateWrapper) > 0;
    }

}
